#include "src/core/generic/datetime.h"
#include <QDebug>

DateTime::string DateTime::toString(const string& format) const
// convert date_time to string using specified format
// inspired by Qt format arguments
/*
d     the day as number without a leading zero (1 to 31)
dd    the day as number with a leading zero (01 to 31)
M     the month as number without a leading zero (1-12)
MM    the month as number with a leading zero (01-12)
yy    the year as two digit number (00-99)
yyyy  the year as four digit number

h     the hour without a leading zero (0 to 23)
hh    the hour with a leading zero (00 to 23)
m     the minute without a leading zero (0 to 59)
mm    the minute with a leading zero (00 to 59)
s     the second without a leading zero (0 to 59)
ss    the second with a leading zero (00 to 59)
z     the milliseconds without leading zeroes (0 to 999)
zzz   the milliseconds with leading zeroes (000 to 999)
*/
{
  string result(format), rawData = boost::posix_time::to_iso_string(time), buffer;
  // rawData = YYYYMMDDTHHMMSS,fffffffff
  size_t pos;
  if ( (pos = result.find("dd")) != string::npos )
  {
    buffer = rawData.substr(6, 2);
    result.replace(pos, 2, buffer);
  }
  if ( (pos = result.find("d")) != string::npos )
  {
    buffer = rawData.substr(6, 2);
    if ( buffer.at(0) == '0' ) buffer.erase(0, 1);
    result.replace(pos, 1, buffer);
  }
  if ( (pos = result.find("MM")) != string::npos )
  {
    buffer = rawData.substr(4, 2);
    result.replace(pos, 2, buffer);
  }
  if ( (pos = result.find("M")) != string::npos )
  {
    buffer = rawData.substr(4, 2);
    if ( buffer.at(0) == '0' ) buffer.erase(0, 1);
    result.replace(pos, 1, buffer);
  }
  if ( (pos = result.find("yyyy")) != string::npos )
  {
    buffer = rawData.substr(0, 4);
    result.replace(pos, 4, buffer);
  }
  if ( (pos = result.find("yy")) != string::npos )
  {
    buffer = rawData.substr(2, 2);
    result.replace(pos, 2, buffer);
  }
  if ( (pos = result.find("hh")) != string::npos )
  {
    buffer = rawData.substr(9, 2);
    result.replace(pos, 2, buffer);
  }
  if ( (pos = result.find("h")) != string::npos )
  {
    buffer = rawData.substr(9, 2);
    if ( buffer.at(0) == '0' ) buffer.erase(0, 1);
    result.replace(pos, 1, buffer);
  }
  if ( (pos = result.find("mm")) != string::npos )
  {
    buffer = rawData.substr(11, 2);
    result.replace(pos, 2, buffer);
  }
  if ( (pos = result.find("m")) != string::npos )
  {
    buffer = rawData.substr(11, 2);
    if ( buffer.at(0) == '0' ) buffer.erase(0, 1);
    result.replace(pos, 1, buffer);
  }
  if ( (pos = result.find("ss")) != string::npos )
  {
    buffer = rawData.substr(13, 2);
    result.replace(pos, 2, buffer);
  }
  if ( (pos = result.find("s")) != string::npos )
  {
    buffer = rawData.substr(13, 2);
    if ( buffer.at(0) == '0' ) buffer.erase(0, 1);
    result.replace(pos, 1, buffer);
  }
  if ( rawData.length() > 16 ) { // have miliseconds
    if ( (pos = result.find("zzz")) != string::npos )
    {
      buffer = rawData.substr(16, 3);
      result.replace(pos, 3, buffer);
    }
    if ( (pos = result.find("z")) != string::npos )
    {
      buffer = rawData.substr(16, 3);
      if ( buffer.at(0) == '0' ) buffer.erase(0, 1);
      if ( buffer.at(0) == '0' ) buffer.erase(0, 1);
      result.replace(pos, 1, buffer);
    }
  } else {
    if ( (pos = result.find("zzz")) != string::npos )
    {
      result.replace(pos, 3, string("000"));
    }
    if ( (pos = result.find("z")) != string::npos )
    {
      result.replace(pos, 1, string("0"));
    }
  }
  return result;
}
